﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.IBizLogic;
using Yunwei.Web.Areas.RRShop.Models;

namespace Yunwei.Web.Areas.RRShop.Controllers
{
    public class DinnerTableController : Controller
    {
        IDinnerTableService _dinnerTableService;

        public DinnerTableController()
        {
            _dinnerTableService = new DinnerTableService();
        }

        // GET: RRShop/DinnerTable
        [Authorize]
        public ActionResult List(int id)
        {
            ViewBag.Id = id;
            var dinnerTables = _dinnerTableService.FindList(1000, "", id, 0);
            List<DinnerTableViewModel> dtvms = new List<DinnerTableViewModel>();

            foreach(var dt in dinnerTables)
            {
                dtvms.Add(DinnerTableViewModel.FromDinnerTable(dt));
            }

            return View(dtvms);
        }

        [Authorize]
        public ActionResult Add(int id)
        {
            ViewBag.Id = id;
            return View(new DinnerTableViewModel()
            {
                Restaurant_Id=id,
            });
        }

        [Authorize]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Add(DinnerTableViewModel dtvm ,int id)
        {
            ViewBag.Id = dtvm.Restaurant_Id;

            if(ModelState.IsValid)
            {
                var dt = _dinnerTableService.Add(dtvm.DinnerTable);
                if (dt != null && dt.Id > 0)
                { 
                    ModelState.AddModelError("", "添加成功");

                    return RedirectToAction("List", new { Id = id });
                }
                else
                    ModelState.AddModelError("", "添加失败");
            }
            
            return View(dtvm);
        }

        [Authorize]
        [HttpGet]
        public ActionResult Edit(int id,int restaurant_Id)
        {
            ViewBag.Id = restaurant_Id;

            var dt = _dinnerTableService.Find(id);
            if (dt == null)
                return RedirectToAction("Error404", "Home", new { Area = "Admin", returnUrl = "/RRShop/DinnerTable/List/" + restaurant_Id });

            return View(DinnerTableViewModel.FromDinnerTable(dt));
        }

        [Authorize]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(DinnerTableViewModel dtvm, int id,int restaurant_Id)
        {
            ViewBag.Id = dtvm.Restaurant_Id;

            if (ModelState.IsValid)
            {
                var isSuccess = _dinnerTableService.Update(dtvm.DinnerTable);
                if (isSuccess)
                {
                    ModelState.AddModelError("", "更新成功");

                    return RedirectToAction("List", new { Id = restaurant_Id });
                }
                else
                    ModelState.AddModelError("", "更新失败");
            }

            return View(dtvm);
        }
    }
}